///接口基类模型
class BaseEntity<T> {
  ///状态码
  late int? code;

  ///泛型数据
  late T? data;

  ///纯列表数据
  late List<T?>? listData=[];


  ///消息
  late String? msg;

  ///response 请求返回值 json 返回的data对象
  ///使用实例类的fromJson 方法解析data数据
  BaseEntity.fromJson(Map<String, dynamic> response,
      {T Function(dynamic json)? fromJsonT}) {
    code = response['code'] as int;
    generateData(response, fromJsonT: fromJsonT);
    msg = response['msg'] as String;
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> json = <String, dynamic>{};
    json['code'] = this.code;
    json['msg'] = this.msg;
    json['data'] = this.data;
    if (listData != null) {
      json['listData'] = listData?.map((v) => v?.toString()).toList();
    }
    return json;
  }

  ///解析data对象
  generateData(Map<String, dynamic> response,
      {T Function(dynamic json)? fromJsonT}) {
    if (fromJsonT != null) {
      if (response['data'] is List) {
        //解析纯列表
        listData = (response['data'] as List).map((e)=>fromJsonT(e)).toList();
      } else {
        //解析map对象
        data = fromJsonT(response['data'] as Map<String, dynamic>);
      }
    } else {
      data=response['data'] as T;
    }
  }
}
